package com.lan.utils;


import java.util.Random;

public class RandomStringUtil {

    private static String c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    /**
     * 根据长度随机生成一段随机字符串,随机内容为大小写字母加数字
     *
     * @param length
     * @return
     */
    public static String generateRandomStrings(int length) {
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char ch = c.charAt(new Random().nextInt(c.length()));
            strBuilder.append(String.valueOf(ch));
        }
        return strBuilder.toString();
    }

    /**
     * 根据长度随机生成一段随机字符串,随机内容可自行设置
     *
     * @param strRange 随机内容选择范围
     * @param length   随机字符串长度
     * @return
     */
    public static String generateRandomStrings(String strRange, int length) {
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char ch = strRange.charAt(new Random().nextInt(strRange.length()));
            strBuilder.append(String.valueOf(ch));
        }
        return strBuilder.toString();
    }

    public static String generateNum(int length) {
        String strRange = "123456789";
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char ch = strRange.charAt(new Random().nextInt(strRange.length()));
            strBuilder.append(String.valueOf(ch));
        }
        return strBuilder.toString();
    }

    /**
     * 随机返回数组的任何一项
     *
     * @param contents
     * @return
     */
    public static String getARandomFromArray(String[] contents) {
        int index = new Random().nextInt(contents.length);
        return contents[index];
    }


    public static void main(String[] args) {
        System.out.println(generateRandomStrings(256));
    }


}
